using System;
using System.Collections.Generic;
using System.Text;
using Activa.CoolStorage;
using CodeStory.Core.Attributes;
using System.ComponentModel;
using CodeStory.WebControls;
using CodeStory.Core.ObjectEntity;
using System.Web.UI.WebControls;
using CodeStory.Core.Services;
using CodeStory.Core.ObjectBusiness;
using CodeStory.Core.Providers;

namespace CodeStory.Core
{
	/// <summary>
	/// This class was used to filter the codestory site setting properties.
	/// </summary>
	[AttributeUsage( AttributeTargets.Property,AllowMultiple=false,Inherited=true)]
	public class CodeStorySiteSettingAttribute : Attribute
	{
	}

	/// <summary>
	/// 
	/// </summary>
	public sealed class CodeStorySiteSetting : IRefreshable
	{
		private string _siteName;
		private string _loginPageUrl;
		private string _homeUrl;
		private string _defaultRegisterUserRoleID;
		private string _styleSheetListString;
		private string defaultContainerName = "default";
		private static CodeStorySiteSetting current;

		[CodeStorySiteSetting]
		[Description("The name of the site.")]
		public string SiteName {
			get { return _siteName; }
			set { _siteName = value; }
		}
		[CodeStorySiteSetting]
		[Description("The name of the site.")]
		public string LoginPageUrl {
			get { return _loginPageUrl; }
			set { _loginPageUrl = value; }
		}
		[CodeStorySiteSetting]
		[Description("The name of the site.")]
		public string HomeUrl {
			get { return _homeUrl; }
			set { _homeUrl = value; }
		}

		[CodeStorySiteSetting]
		[PropertyEditor(typeof(UserRoleEditor))]
		public string DefaultRegisterUserRoleID {
			get { return _defaultRegisterUserRoleID; }
			set { _defaultRegisterUserRoleID = value; }
		}

		[CodeStorySiteSetting]
		[Description("The stylesheets of the site.")]
		public string StyleSheetListString {
			get { return _styleSheetListString; }
			set { _styleSheetListString = value; }
		}
		[PropertyEditor(typeof(ContainerEditor))]
		[CodeStorySiteSetting]
		[Description("The portlet's default container of the site.")]
		public string DefaultContainerName {
			get { return defaultContainerName; }
			set { defaultContainerName = value; }
		}

		#region Save & Read
		static readonly object helpobject = new object();
		public static CodeStorySiteSetting Current {
			get {
				lock (helpobject) {
					if (current == null) {
						current = new CodeStorySiteSetting();
						string name = "site";
						SiteSettingBusiness.LoadObject(name, current);
					}
				}
				return current;
			}
		}

		
		public void Save() {
			SiteSettingBusiness.SaveObject("site", this);
		}

		//public void Save() {
		//    Dictionary<string, string> dict = ObjectPersisteService.SaveObject(this);
		//    foreach (KeyValuePair<string, string> pair in dict) {
		//        InsertOrUpdateSiteSetting(pair.Key, pair.Value);
		//    }
		//}

		//private void InsertOrUpdateSiteSetting(string key, string val) {
		//    SiteSettingObject obj = SiteSettingObject.ReadUsingUniqueField("RegisterKey", key);
		//    if (obj == null) {
		//        obj = SiteSettingObject.New();
		//        obj.RegisterKey = key;
		//    }
		//    obj.RegisterValue = val;
		//    obj.Save();
		//} 
		#endregion

		#region IRefreshable Members

		public void Refresh() {
			//throw new Exception("The method or operation is not implemented.");
			current = null;
		}

		#endregion
	}

	public class UserRoleEditor : DropDownPropertyEditor
	{
		public override void Prepare() {
			Items.Clear();
			//bool bound = false;
			//if (Page != null) {
			//    if (!Page.IsPostBack) {
			//        bound = true;
			//    }
			//} else {
			//    bound = true;
			//}
			//if (bound) {
			
				foreach (RoleObject role in RoleObject.List()) {
					ListItem item = new ListItem(role.RoleName, role.RoleID.ToString());
					Items.Add(item);
				}
			//}
		}
	}

	public class ContainerEditor : DropDownPropertyEditor
	{
		public override void Prepare() {
			Items.Clear();
			ListItem em = new ListItem("(empty)", string.Empty);
			foreach (PortletTemplateObject obj in PortletTemplateObject.List()) {
				ListItem item = new ListItem(obj.FriendlyName, obj.CodeName);
				Items.Add(item);
			}
		}
	}
}
